<?php
namespace admin\controllers;

use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use admin\models\LoginForm;

/**
 * Site controller
 */
class SiteController extends Controller
{
	public $layout = FALSE; //关闭layout布局
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['login', 'error','captcha'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['logout', 'index','serverinfo'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
	        'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
        	'captcha' => [ 
        		'class' => 'yii\captcha\CaptchaAction',
        		'maxLength' => 4,
        		'minLength' => 4,
        		'height' => 37,
        		'width' =>115,
        		//'transparent' => true, 透明
        		//'foreColor' => '',
        		'offset' => 10 //字符偏移量
        	],
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionLogin()
    {   
        $this->layout = false;
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        } else {
            return $this->render('login', [
                'model' => $model,
                'title' => '登录后台'
            ]);
        }
    }

    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }
	
	public function actionServerinfo(){
        $mysql = Yii::$app->db->createCommand('SELECT VERSION() as version')
            ->queryAll();
			
		return $this->render('serverinfo',['mysql_version'=>$mysql[0]['version']]);
    }
}
